package com.easy.frame.servers.sys.service.impl.process;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.frame.base.api.pojo.PageDTO;
import com.easy.frame.base.api.pojo.PageVO;
import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.basic.constant.DbConstants;
import com.easy.frame.base.basic.enums.ResponseCodeEnum;
import com.easy.frame.base.basic.util.AuthUtils;
import com.easy.frame.base.service.mp.page.PageProxy;
import com.easy.frame.servers.sys.dto.SysCodeDefDTO;
import com.easy.frame.servers.sys.entity.SysCodeDef;
import com.easy.frame.servers.sys.mapper.process.SysCodeDefMapper;
import com.easy.frame.servers.sys.service.process.SysCodeDefService;
import com.easy.frame.servers.sys.vo.SysCodeDefVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 编码定义表 服务实现类
 *
 * @author tp
 * @since 2021-01-05
 */
@Service
public class SysCodeDefServiceImpl extends ServiceImpl<SysCodeDefMapper, SysCodeDef> implements SysCodeDefService {

    @Resource
    SysCodeDefMapper sysCodeDefMapper;

    @Override
    public PageVO<SysCodeDefVO> selectForPage(PageDTO<SysCodeDefDTO> pageDTO) {
        PageVO<SysCodeDefVO> proxy = PageProxy.proxy(baseMapper::selectForPage, pageDTO);
        List<SysCodeDefVO> records = proxy.getRecords();
        ServiceResponse<List<SysCodeDefVO>> success = new ServiceResponse<>(records, ResponseCodeEnum.SUCCESS);
        proxy.setRecords(success.getData());
        return proxy;
    }

    @Override
    public SysCodeDef selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int countById(Long id) {
        return sysCodeDefMapper.selectCount(
                new LambdaQueryWrapper<SysCodeDef>()
                        .eq(SysCodeDef::getId, id)
                        .eq(SysCodeDef::getDeleted, DbConstants.DEFAULT_NO)
        );
    }

    @Override
    public int updateDeleted(Long id) {
        SysCodeDef sysCodeDef = new SysCodeDef();
        sysCodeDef.setId(id);
        sysCodeDef.setDeleted(DbConstants.DEFAULT_YES);
        return sysCodeDefMapper.updateById(sysCodeDef);
    }

    @Override
    public SysCodeDefVO selectVOById(Long id) {
        return sysCodeDefMapper.selectVOById(id);
    }

    @Override
    public void updateEntityById(SysCodeDef entity) {
        entity.setUpdateBy(AuthUtils.getCurrentUserId());
        entity.setUpdateTime(new Date());
        updateById(entity);
    }
}
